package com.wen.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wen.yygh.vo.hosp.DepartmentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import com.wen.yygh.hosp.repository.DepartmentRepository;
import com.wen.yygh.hosp.service.DepartmentService;
import com.wen.yygh.model.hosp.Department;
import com.wen.yygh.vo.hosp.DepartmentQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Wenjl
 */
@Service
@Slf4j
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> paramMap) {
        //将map对象转换为字符串
        String s = JSONObject.toJSONString(paramMap);
        //将字符串转换为对象
        Department department = JSONObject.parseObject(s, Department.class);
        //判断是否存在数据
        Department departmentExits = departmentRepository.findDepartmenByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if (departmentExits != null) {
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);

        } else {
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            department.setUpdateTime(new Date());
        }
        departmentRepository.save(department);

    }

    @Override
    public Page<Department> findPageDepartment(Integer pageNum, Integer pageSize, DepartmentQueryVo departmentQueryVo) {
        //创建Pageable对象，设置当前页和每页记录数
        //0是第一页
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        //创建Example对象
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Department department = new Department();

        department.setIsDeleted(0);

        BeanUtils.copyProperties(departmentQueryVo, department);
        Example<Department> example = Example.of(department, matcher);
        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }

    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.findDepartmenByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            //如果存在则删除
            departmentRepository.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> getDepartmentListByHoscode(String hoscode) {
        //最后返回的结果集
        List<DepartmentVo> result = new ArrayList<>();
        //根据医院编号查询除所有科室
        Department queryVo = new Department();
        queryVo.setHoscode(hoscode);
        Example example = Example.of(queryVo);
        //所有科室列表
        List<Department> departmentList = departmentRepository.findAll(example);
        //根据大科室编号，获取每个大科室对应的小科室
        Map<String, List<Department>> collect = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        for (Map.Entry<String, List<Department>> entry : collect.entrySet()) {
            //大科室编号
            String key = entry.getKey();
            //大客室对应的所有科室列表
            List<Department> value = entry.getValue();
            //封装大科室
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(key);
            departmentVo.setDepname(value.get(0).getBigname());
            //封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for (Department department : value) {
                //小科室
                DepartmentVo child = new DepartmentVo();
                child.setDepname(department.getDepname());
                child.setDepcode(department.getDepcode());
                children.add(child);
            }
            //将小科室列表封装进列表
            departmentVo.setChildren(children);
            //将科室列表封装到结果集中
            result.add(departmentVo);
        }
        log.info("result=>" + result);
        return result;
    }

    @Override
    public String findDeptName(String depcode) {
        Department department = departmentRepository.findDepartmentByDepcode(depcode);

        return  department==null?" ":department.getDepname();
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        Department department = departmentRepository.findDepartmenByHoscodeAndDepcode(hoscode, depcode);
        return department;
    }


}
